﻿using System.Configuration;
using UFIDA.U9.WSS.ProxyService.Behavior.WebHttp.Base;

namespace UFIDA.U9.WSS.ProxyService.Behavior.WebHttp
{
    public class ContextProxyServiceBehaviorExtension : NewtonsoftJsonBehaviorExtension<ContextProxyServiceBehavior>
    {
        /// <summary>
        ///     是否使用DataContract解析
        /// </summary>
        [ConfigurationProperty("useDataContract", DefaultValue = false, Options = ConfigurationPropertyOptions.None)]
        public bool UseDataContract
        {
            get
            {
                if (this["useDataContract"] == null || this["useDataContract"].ToString() == string.Empty) return false;
                return bool.Parse(this["useDataContract"].ToString());
            }
        }

        /// <summary>
        ///     代理是否需要授权
        /// </summary>
        [ConfigurationProperty("accessProxy", DefaultValue = false, Options = ConfigurationPropertyOptions.None)]
        public bool AccessProxy
        {
            get
            {
                if (this["accessProxy"] == null || this["accessProxy"].ToString() == string.Empty) return false;
                return bool.Parse(this["accessProxy"].ToString());
            }
        }

        /// <summary>
        ///     代理授权分组名
        /// </summary>
        [ConfigurationProperty("accessGroup", DefaultValue = "", Options = ConfigurationPropertyOptions.None)]
        public string AccessGroup
        {
            get { return this["accessGroup"] != null ? this["accessGroup"] as string : string.Empty; }
        }

        /// <summary>
        ///     上下文是否需要密码
        /// </summary>
        [ConfigurationProperty("contextNeedUserPassword", DefaultValue = false,
            Options = ConfigurationPropertyOptions.None)]
        public bool ContextNeedUserPassword
        {
            get
            {
                if (this["contextNeedUserPassword"] == null ||
                    this["contextNeedUserPassword"].ToString() == string.Empty)
                    return false;
                return bool.Parse(this["contextNeedUserPassword"].ToString());
            }
        }

        protected override object CreateBehavior()
        {
            return new ContextProxyServiceBehavior(UseDataContract, AccessProxy, AccessGroup, ContextNeedUserPassword);
        }
    }
}